<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Coupon;
use think\Db;
use think\Log;
use think\Request;

/**
 * 买卖交易接口
 */
class Mall extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [
        'allServiceList',
        'allDemandList',
        'detailsDemand',
        'allWangBuyList',
        'detailsWangBuy',
        'detailsService',
        'configure',
        'promotionPackageList',
        'reChargeResult',
        'reMemberResult',
        'demandOrderResult',
        'alipayReChargeResult',
        'alipayMemberResult',
        'alipayDemandOrderResult',
        'alipayPublishServiceResult',
        'publishServiceOrderResult',
        'alipayPublishWantToBuyResult',
        'publishWantToBuyOrderResult',
        'alipayPublishDemandResult',
        'publishDemandOrderResult',
    ];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //根据code获取id
    protected function getAreaid($code = ''){
        if(!$code){
            $this->error('code必传');
        }

        $info = db('area')->where('areacode',$code)->find();

        return $info;
    }

    /**
     * 敏感词检测
     * @param $list 敏感词组
     * @param $string 检测字符串
     * @return string
     */
    protected function sensitive($string){
        $list = db('sensitive_words')->column('name');
        $badword1 = array_combine($list,array_fill(0,count($list),'****'));
        $str = strtr($string, $badword1);
        return $str;
    }

    /***
     * 需求筛选条件
     */
    public function configure(){
        $years = db('demand')->where('years','not null')->group('years')->column('years');

        $tags = db('tag')->column('name');

        $moneys = [
            [
                'value' => '0-1000',
                'name' => '0~1000元',
            ],
            [
                'value' => '1000-5000',
                'name' => '1000~5000元',
            ],
            [
                'value' => '5000-10000',
                'name' => '5000~10000元',
            ],
            [
                'value' => '10000-20000',
                'name' => '10000~20000元',
            ],
            [
                'value' => '50000',
                'name' => '50000元及以上',
            ],
            [
                'value' => '100000',
                'name' => '100000元及以上',
            ],
            [
                'value' => '500000',
                'name' => '500000万及以上',
            ],
        ];

        $data = [
            'years' => $years,
            'tags' => $tags,
            'moneys' => $moneys,
        ];

        $this->success('',$data);
    }

    /***
     * 全部需求列表
     */
    public function allDemandList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);
        $where = [];

        input('param.one_category_id') && $where['one_category_id'] = ['=',input('param.one_category_id')];
        input('param.two_category_id') && $where['two_category_id'] = ['=',input('param.two_category_id')];
        input('param.tree_category_id') && $where['tree_category_id'] = ['=',input('param.tree_category_id')];
        input('param.province_code') && $where['province_code'] = ['=',input('param.province_code')];
        input('param.years') && $where['years'] = ['=',input('param.years')];
        input('param.tag') && $where['tag'] = ['=',input('param.tag')];
        input('param.name') && $where['name|title'] = ['like','%'.input('param.name').'%'];
        input('param.city_code') && $where['city_code'] = ['=',input('param.city_code')];
        input('param.region_code') && $where['region_code'] = ['=',input('param.region_code')];

        if(input('param.money')){
            if(strpos(input('param.money'),'-') !== false){
                $money = explode('-', input('param.money'));
                $where['money'] = ['BETWEEN', [$money[0], $money[1]]];
            }else{
                $where['money'] = ['>=',input('param.money')];
            }
        }

        $where['to_examine'] = 1;
        $list = db('demand')->where('status','normal')->where($where)->order('is_top desc,createtime desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['one_category_name'] = db('category')->where('id',$value['one_category_id'])->value('name');
            $data['data'][$key]['two_category_name'] = db('category')->where('id',$value['two_category_id'])->value('name');
            $data['data'][$key]['tree_category_name'] = db('category')->where('id',$value['tree_category_id'])->value('name');
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            if($user && $user->is_member == 1){

            }else{
                $data['data'][$key]['contacts_tel'] = substr_replace($value['contacts_tel'], "********", 3, 7);;
            }
        }
        $this->success('',$data);
    }

    /**
     * 需求详情
     */
    public function detailsDemand(){
        $id = input('param.id');
        $user = $this->auth->getUser();

        $info = db('demand')->where('id',$id)->find();
        if($user && ($user->is_member == 1 || $info['user_id'] == $user->id)){

        }else{
            $info['contacts_tel'] = substr_replace($info['contacts_tel'], "********", 3, 7);;
        }

        if($user){
            $order_info = db('demand_order')->where('status','normal')->where('demand_id',$info['id'])->where('pay_status',2)->where('user_id',$user->id)->find();
            if($order_info){
                $info['status'] = 'normal';
            }
        }

        $info['one_category_name'] = db('category')->where('id',$info['one_category_id'])->value('name');
        $info['two_category_name'] = db('category')->where('id',$info['two_category_id'])->value('name');
        $info['tree_category_name'] = db('category')->where('id',$info['tree_category_id'])->value('name');
        $this->success('成功',$info);
    }

    /**
     * 我发布的需求列表
     */
    public function myDemandList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $list = db('demand')->where('status','normal')->where('user_id',$user->id)->order('id desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            $value['is_extension'] == 1 && $data['data'][$key]['extension_end_time_text'] = date('Y-m-d H:i:s',$value['extension_end_time']);
            $value['is_top'] == 1 && $data['data'][$key]['top_end_time_text'] = date('Y-m-d H:i:s',$value['top_end_time']);
        }
        $this->success('',$data);
    }


    /**
     * 发布需求
     */
    public function addDemand(){
        if(request()->isPost()){
            $user = $this->auth->getUser();

            $province = $this->getAreaid(input('param.province_code'));
            $city = $this->getAreaid(input('param.city_code'));
            $region = $this->getAreaid(input('param.region_code'));
            $all = Request::instance()->param(false);
            $data['name'] = $this->sensitive(input('param.name'));
            $data['title'] = $this->sensitive(input('param.title'));
            $data['describe'] = input('param.describe');
            $data['one_category_id'] = input('param.one_category_id');
            $data['two_category_id'] = input('param.two_category_id');
            $data['tree_category_id'] = input('param.tree_category_id');
            $data['user_id'] = $user->id;
            $data['province_code'] = input('param.province_code');
            $data['city_code'] = input('param.city_code');
            $data['region_code'] = input('param.region_code');
            $data['province_id'] = $province['id'];
            $data['city_id'] = $city['id'];
            $data['region_id'] = $region['id'];
            $data['province_name'] = $province['name'];
            $data['city_name'] = $city['name'];
            $data['region_name'] = $region['name'];
            $data['years'] = input('param.years');
            $data['money'] = input('param.money');

            if($data['money'] <= 0){
                $this->success('金额必须大于0元');
            }

            $data['tag'] = input('param.tag');
            $data['logo'] = input('param.logo');
            $data['imgs'] = input('param.imgs');
            $data['contacts'] = input('param.contacts');
            $data['contacts_tel'] = input('param.contacts_tel');
            if(isset($all['content'])){
                $data['content'] = $this->sensitive($all['content']);
            }
            $data['remarks'] = input('param.remarks');
            $data['status'] = input('param.status');
            $data['createtime'] = time();

            $publish_configuration = db('config')->where('group','publish_configuration')->column('value','name');
            $start_time = strtotime(date("Y-m-d",time()));
            $end_time = $start_time+60*60*24;
            $count = db('demand')->where('user_id',$user->id)->where('createtime','between',[$start_time,$end_time])->count();
            if($count >= $publish_configuration['publish_number']){
                $data['pay_price'] = $publish_configuration['publish_money'] <= 0 ? 0.1 : $publish_configuration['publish_money'];
                $data['order_code'] = orderNo();
                $data['status'] = 'deleted';
                $result = db('demand')->insert($data);
                if($result){
                    $this->error('免费条数已超,需要支付',['order_code' => $data['order_code']],'-456');
                }
            }else{
                $result = db('demand')->insert($data);
                if($result){
                    $this->success('成功');
                }else{
                    $this->error('失败');
                }
            }
        }
    }

    /***
     * 修改需求
     */
    public function editDemand(){
        if(request()->isPost()){
            $province = $this->getAreaid(input('param.province_code'));
            $city = $this->getAreaid(input('param.city_code'));
            $region = $this->getAreaid(input('param.region_code'));
            $all = Request::instance()->param(false);
            $user = $this->auth->getUser();
            $data['name'] = $this->sensitive(input('param.name'));
            $data['title'] = $this->sensitive(input('param.title'));
            $data['describe'] = input('param.describe');
            $data['one_category_id'] = input('param.one_category_id');
            $data['two_category_id'] = input('param.two_category_id');
            $data['tree_category_id'] = input('param.tree_category_id');
            $data['user_id'] = $user->id;
            $data['province_code'] = input('param.province_code');
            $data['city_code'] = input('param.city_code');
            $data['region_code'] = input('param.region_code');
            $data['province_id'] = $province['id'];
            $data['city_id'] = $city['id'];
            $data['region_id'] = $region['id'];
            $data['province_name'] = $province['name'];
            $data['city_name'] = $city['name'];
            $data['region_name'] = $region['name'];
            $data['years'] = input('param.years');
            $data['money'] = input('param.money');
            if($data['money'] <= 0){
                $this->success('金额必须大于0元');
            }

            $data['tag'] = input('param.tag');
            $data['logo'] = input('param.logo');
            $data['imgs'] = input('param.imgs');
            $data['contacts'] = input('param.contacts');
            $data['contacts_tel'] = input('param.contacts_tel');
            if(isset($all['content'])){
                $data['content'] = $this->sensitive($all['content']);
            }
            $data['remarks'] = input('param.remarks');
            $data['status'] = input('param.status');
            $data['updatetime'] = time();
            $id = input('param.id');

            $info = db('demand')->where('user_id',$user->id)->where('id',$id)->find();
            if ($info['status'] != 'normal' && $info['status'] != 'hidden'){
                $this->error('该订单的状态不符合修改条件');
            }

            $result = db('demand')->where('id',$id)->update($data);
            if($result){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
    }

    /**
     * 删除需求
     */
    public function delDemand(){
        $id = input('param.id');
        $user = $this->auth->getUser();

        $info = db('demand')->where('user_id',$user->id)->where('id',$id)->find();
        if ($info['status'] != 'hidden'){
            $this->error('订单状态不可删除');
        }

        $result = db('demand')->where('id',$id)->update(['status' => 'deleted','deletetime' => time()]);
        if($result){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    /**
     * 发布求购
     */
    public function addWangBuy(){
        if(request()->isPost()){
            $province = $this->getAreaid(input('param.province_code'));
            $city = $this->getAreaid(input('param.city_code'));
            $region = $this->getAreaid(input('param.region_code'));

            $user = $this->auth->getUser();
            $data['name'] = $this->sensitive(input('param.name'));
            $data['title'] = $this->sensitive(input('param.title'));
            $data['describe'] = input('param.describe');
            $data['user_id'] = $user->id;
            $data['province_code'] = input('param.province_code');
            $data['city_code'] = input('param.city_code');
            $data['region_code'] = input('param.region_code');
            $data['province_id'] = $province['id'];
            $data['city_id'] = $city['id'];
            $data['region_id'] = $region['id'];
            $data['province_name'] = $province['name'];
            $data['city_name'] = $city['name'];
            $data['region_name'] = $region['name'];
            $data['contacts'] = input('param.contacts');
            $data['contacts_tel'] = input('param.contacts_tel');
            $data['remarks'] = input('param.remarks');
            $data['status'] = input('param.status');
            $data['createtime'] = time();

            $publish_configuration = db('config')->where('group','publish_configuration')->column('value','name');
            $start_time = strtotime(date("Y-m-d",time()));
            $end_time = $start_time+60*60*24;
            $count = db('want_to_buy')->where('user_id',$user->id)->where('createtime','between',[$start_time,$end_time])->count();
            if($count >= $publish_configuration['publish_number']){
                $data['pay_price'] = $publish_configuration['publish_money'] <= 0 ? 0.1 : $publish_configuration['publish_money'];
                $data['order_code'] = orderNo();
                $data['status'] = 'deleted';
                $result = db('want_to_buy')->insert($data);
                if($result){
                    $this->error('免费条数已超,需要支付',['order_code' => $data['order_code']],'-456');
                }
            }else{
                $result = db('want_to_buy')->insert($data);
                if($result){
                    $this->success('成功');
                }else{
                    $this->error('失败');
                }
            }
        }
    }

    /***
     * 修改求购
     */
    public function editWangBuy(){
        if(request()->isPost()){
            $province = $this->getAreaid(input('param.province_code'));
            $city = $this->getAreaid(input('param.city_code'));
            $region = $this->getAreaid(input('param.region_code'));

            $user = $this->auth->getUser();
            $data['name'] = $this->sensitive(input('param.name'));
            $data['title'] = $this->sensitive(input('param.title'));
            $data['describe'] = input('param.describe');
            $data['user_id'] = $user->id;
            $data['province_code'] = input('param.province_code');
            $data['city_code'] = input('param.city_code');
            $data['region_code'] = input('param.region_code');
            $data['province_id'] = $province['id'];
            $data['city_id'] = $city['id'];
            $data['region_id'] = $region['id'];
            $data['province_name'] = $province['name'];
            $data['city_name'] = $city['name'];
            $data['region_name'] = $region['name'];
            $data['contacts'] = input('param.contacts');
            $data['contacts_tel'] = input('param.contacts_tel');
            $data['remarks'] = input('param.remarks');
            $data['status'] = input('param.status');
            $data['updatetime'] = time();
            $id = input('param.id');

            $result = db('want_to_buy')->where('id',$id)->update($data);
            if($result){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
    }

    /***
     * 删除求购
     */
    public function delWangBuy(){
        $id = input('param.id');
        $user = $this->auth->getUser();

        $result = db('want_to_buy')->where('user_id',$user->id)->where('id',$id)->update(['status' => 'deleted','deletetime' => time()]);
        if($result){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    /***
     * 全部求购列表
     */
    public function allWangBuyList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $where = [];
        input('param.province_code') && $where['province_code'] = ['=',input('param.province_code')];
        input('param.city_code') && $where['city_code'] = ['=',input('param.city_code')];
        input('param.region_code') && $where['region_code'] = ['=',input('param.region_code')];
        input('param.name') && $where['name|title'] = ['like','%'.input('param.name').'%'];

        $where['to_examine'] = 1;
        $list = db('want_to_buy')->where('status','normal')->where($where)->order('is_top desc,createtime desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            if($user && $user->is_member == 1){

            }else{
                $data['data'][$key]['contacts_tel'] = substr_replace($value['contacts_tel'], "********", 3, 7);;
            }
        }
        $this->success('',$data);
    }

    /**
     * 求购详情
     */
    public function detailsWangBuy(){
        $user = $this->auth->getUser();
        $id = input('param.id');

        $info = db('want_to_buy')->where('id',$id)->find();
        if($user && ($user->is_member == 1 || $info['user_id'] == $user->id)){

        }else{
            $info['contacts_tel'] = substr_replace($info['contacts_tel'], "********", 3, 7);;
        }
        $this->success('成功',$info);
    }

    /**
     * 我发布的求购列表
     */
    public function myWangBuyList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $list = db('want_to_buy')->where('status','normal')->where('user_id',$user->id)->order('id desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            $value['is_extension'] == 1 && $data['data'][$key]['extension_end_time_text'] = date('Y-m-d H:i:s',$value['extension_end_time']);
            $value['is_top'] == 1 && $data['data'][$key]['top_end_time_text'] = date('Y-m-d H:i:s',$value['top_end_time']);
        }
        $this->success('',$data);
    }

    /**
     * 发布服务
     */
    public function addService(){
        if(request()->isPost()){
            $province = $this->getAreaid(input('param.province_code'));
            $city = $this->getAreaid(input('param.city_code'));
            $region = $this->getAreaid(input('param.region_code'));

            $user = $this->auth->getUser();
            $data['title'] = $this->sensitive(input('param.title'));
            $data['user_id'] = $user->id;
            $data['province_code'] = input('param.province_code');
            $data['city_code'] = input('param.city_code');
            $data['region_code'] = input('param.region_code');
            $data['province_id'] = $province['id'];
            $data['city_id'] = $city['id'];
            $data['region_id'] = $region['id'];
            $data['province_name'] = $province['name'];
            $data['city_name'] = $city['name'];
            $data['region_name'] = $region['name'];
            $data['contacts'] = input('param.contacts');
            $data['contacts_tel'] = input('param.contacts_tel');
            $data['remarks'] = input('param.remarks');
            $data['synopsis'] = input('param.synopsis');
            $data['status'] = input('param.status');
            $all = Request::instance()->param(false);
            if(isset($all['content'])){
                $data['content'] = $this->sensitive($all['content']);
            }
            $data['createtime'] = time();

            $publish_configuration = db('config')->where('group','publish_configuration')->column('value','name');
            $start_time = strtotime(date("Y-m-d",time()));
            $end_time = $start_time+60*60*24;
            $count = db('service')->where('user_id',$user->id)->where('createtime','between',[$start_time,$end_time])->count();
            if($count >= $publish_configuration['publish_number']){
                $data['pay_price'] = $publish_configuration['publish_money'] <= 0 ? 0.1 : $publish_configuration['publish_money'];
                $data['order_code'] = orderNo();
                $data['status'] = 'deleted';
                $result = db('service')->insert($data);
                if($result){
                    $this->error('免费条数已超,需要支付',['order_code' => $data['order_code']],'-456');
                }
            }else{
                $result = db('service')->insert($data);
                if($result){
                    $this->success('成功');
                }else{
                    $this->error('失败');
                }
            }
        }
    }

    /**
     * 修改服务
     */
    public function editService(){
        if(request()->isPost()){
            $province = $this->getAreaid(input('param.province_code'));
            $city = $this->getAreaid(input('param.city_code'));
            $region = $this->getAreaid(input('param.region_code'));

            $user = $this->auth->getUser();
            $data['title'] = $this->sensitive(input('param.title'));
            $data['user_id'] = $user->id;
            $data['province_code'] = input('param.province_code');
            $data['city_code'] = input('param.city_code');
            $data['region_code'] = input('param.region_code');
            $data['province_id'] = $province['id'];
            $data['city_id'] = $city['id'];
            $data['region_id'] = $region['id'];
            $data['province_name'] = $province['name'];
            $data['city_name'] = $city['name'];
            $data['region_name'] = $region['name'];
            $data['contacts'] = input('param.contacts');
            $data['contacts_tel'] = input('param.contacts_tel');
            $data['remarks'] = input('param.remarks');
            $data['synopsis'] = input('param.synopsis');
            $data['status'] = input('param.status');
            $all = Request::instance()->param(false);
            if(isset($all['content'])){
                $data['content'] = $this->sensitive($all['content']);
            }
            $data['updatetime'] = time();
            $id = input('param.id');

            $result = db('service')->where('id',$id)->update($data);
            if($result){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
    }

    /**
     * 删除服务
     */
    public function delService(){
        $id = input('param.id');
        $user = $this->auth->getUser();

        $result = db('service')->where('user_id',$user->id)->where('id',$id)->update(['status' => 'deleted','deletetime' => time()]);
        if($result){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    /***
     * 全部服务列表
     */
    public function allServiceList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $where = [];
        input('param.province_code') && $where['province_code'] = ['=',input('param.province_code')];
        input('param.city_code') && $where['city_code'] = ['=',input('param.city_code')];
        input('param.region_code') && $where['region_code'] = ['=',input('param.region_code')];
        input('param.name') && $where['title'] = ['like','%'.input('param.name').'%'];

        $where['to_examine'] = 1;
        $list = db('service')->where('status','normal')->where($where)->order('is_top desc,createtime desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            if($user && $user->is_member == 1){

            }else{
                $data['data'][$key]['contacts_tel'] = substr_replace($value['contacts_tel'], "********", 3, 7);;
            }

        }
        $this->success('',$data);
    }

    /**
     * 服务详情
     */
    public function detailsService(){
        $id = input('param.id');
        $user = $this->auth->getUser();

        $info = db('service')->where('id',$id)->find();
        if($user && ($user->is_member == 1 || $info['user_id'] == $user->id)){

        }else{
            $info['contacts_tel'] = substr_replace($info['contacts_tel'], "********", 3, 7);;
        }
        $this->success('成功',$info);
    }

    /**
     * 我发布的服务列表
     */
    public function myServiceList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $list = db('service')->where('status','normal')->where('user_id',$user->id)->order('id desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
            $value['is_extension'] == 1 && $data['data'][$key]['extension_end_time_text'] = date('Y-m-d H:i:s',$value['extension_end_time']);
            $value['is_top'] == 1 && $data['data'][$key]['top_end_time_text'] = date('Y-m-d H:i:s',$value['top_end_time']);
        }
        $this->success('',$data);
    }

    /***
     * 推广/置顶套餐列表
     */
    public function promotionPackageList(){
        $list = db('promotion_package')->where('status','normal')->select();

        $this->success('',$list);
    }

    /***
     * 支付宝扫码付款
     * @param string $pay_code 支付单号
     * @param string $money 金额(单位:元)
     * @param string $notify_url 回调地址
     * @param string $remark 充值说明
     * @throws \Exception
     */
    private function aliPayQrcode($pay_code = '',$money = '',$notify_url = '',$remark = ''){
        $ali_pay_config = db('config')->where('group', 'ali_pay')->column('value', 'name');

        require EXTEND_PATH.'/alipay/aop/AopClient.php';
        require EXTEND_PATH.'/alipay/aop/request/AlipayTradePrecreateRequest.php';
        $aop = new \AopClient ();
        $request = new \AlipayTradePrecreateRequest();

        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $ali_pay_config['alipay_appid'];
        $aop->rsaPrivateKey = $ali_pay_config['alipay_private_key'];
        $aop->alipayrsaPublicKey = $ali_pay_config['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';

        $object = new \stdClass();
        $object->out_trade_no = $pay_code;
        $object->total_amount = $money;
        $object->subject = $remark;
        $object->qr_code_timeout_express = '10m';

        $json = json_encode($object);
        $request->setNotifyUrl($notify_url);
        $request->setBizContent($json);
        $result = $aop->execute ( $request);
        return $result;
    }

    //支付宝推广/置顶套餐下单回调地址
    public function alipayReChargeResult(){
        $params = input('param.');
        Log::info('支付宝充值回调参数');
        Log::info($params);
        if($params['trade_status'] == 'TRADE_SUCCESS'){
            Db::startTrans();

            $info = db('promotion_order')->where('order_code',$params['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $promotion_data['transaction_id'] = $params['trade_no'];
                $promotion_data['openid'] = $params['buyer_id'];
                $promotion_data['total'] = $params['total_amount'];
                $promotion_data['payer_time'] = time();
                $promotion_data['pay_status'] = 1;

                $p_order = db('promotion_order')->where('id',$info['id'])->update($promotion_data);
                if($p_order) {
                    $promotion_package_info = db('promotion_package')->where('id', $info['promotion_package_id'])->find();
                    if ($info['type'] == 1) {
                        $p_order_data['is_extension'] = 1;
                        $p_order_data['extension_end_time'] = time() + (86400 * $promotion_package_info['number']);
                    } else {
                        $p_order_data['is_top'] = 1;
                        $p_order_data['top_end_time'] = time() + (86400 * $promotion_package_info['number']);
                    }

                    switch ($info['promotion_type']) {
                        case 1:
                            db('demand')->where('id', $info['promotion_id'])->update($p_order_data);
                            break;
                        case 2:
                            db('want_to_buy')->where('id', $info['promotion_id'])->update($p_order_data);
                            break;
                        case 3:
                            db('service')->where('id', $info['promotion_id'])->update($p_order_data);
                            break;
                    }

                    Db::commit();
                }
            }else{
                Db::rollback();
            }
        }
    }

    /***
     * 微信扫码付款
     * @param string $pay_code 支付单号
     * @param string $money 金额(单位:元)
     * @param string $notify_url 回调地址
     * @param string $remark 充值说明
     * @return mixed
     */
    private function wechatPayQrcode($pay_code = '',$money = '',$notify_url = '',$remark = ''){
        $wechat_config = db('config')->where('group','wechat_pay')->column('value','name');
        return controller('WechatPay')->wechatPayt($wechat_config,$pay_code,$money * 100,$notify_url,$remark);
    }

    //置顶或者推广下单
    public function reCharge(){
        if(request()->isPost()){
            $user = $this->auth->getUser();
            $data['type'] = input('param.type');
            $data['promotion_type'] = input('param.promotion_type');
            $data['promotion_id'] = input('param.promotion_id');
            $data['promotion_package_id'] = input('param.promotion_package_id');
            $promotion_package_info = db('promotion_package')->where('id',$data['promotion_package_id'])->find();
            if(!$promotion_package_info){
                $this->error('未找到匹配的套餐');
            }
            $money = $promotion_package_info['money'];
            $data['order_code'] = orderNo();
            $data['createtime'] = time();
            $data['money'] = $money;
            $data['user_id'] = $user->id;

            $data['pay_type'] = input('param.pay_type',1);
            if($data['pay_type'] == 1){
                $notify = request()->domain()."/api/mall/reChargeResult";
                $result = db('promotion_order')->insert($data);
                if($result){
                    $wechat_result = $this->wechatPayQrcode($data['order_code'],$data['money'],$notify,'微信扫码购买服务');
                    $this->success('',['order_code' => $data['order_code'],'code_url' => $wechat_result['code_url']]);
                }else{
                    $this->error('系统错误,请联系管理员');
                }
            }else{
                $result = db('promotion_order')->insert($data);
                if($result){
                    $notify_url = request()->domain()."/api/mall/alipayReChargeResult";
                    $result = $this->aliPayQrcode($data['order_code'],$data['money'],$notify_url,'支付宝扫码购买服务');
                    $this->success('',$result);
                }else{
                    $this->error();
                }
            }
        }
    }

    //推广回调地址
    public function reChargeResult(){
        $postXml = file_get_contents("php://input");    // 接受通知参数；
        Log::info('回调参数查看1');
        Log::info($postXml);
        db('wechat_pay_log')->insert(['text'=>$postXml]);

        $data = json_decode($postXml,true);
        if(isset($data['event_type']) && $data['event_type'] == "TRANSACTION.SUCCESS"){
            $result = controller('WechatPay')->decryptCiphertext($data);
            Log::info('回调参数查看2');
            Log::info($result);

            Db::startTrans();

            $info = db('promotion_order')->where('order_code',$result['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $promotion_data['transaction_id'] = $result['transaction_id'];
                $promotion_data['openid'] = $result['payer']['openid'];
                $promotion_data['total'] = $result['amount']['total'];
                $promotion_data['payer_time'] = time();
                $promotion_data['pay_status'] = 1;

                $p_order = db('promotion_order')->where('id',$info['id'])->update($promotion_data);
                if($p_order){
                    $promotion_package_info = db('promotion_package')->where('id',$info['promotion_package_id'])->find();
                    if($info['type'] == 1){
                        $p_order_data['is_extension'] = 1;
                        $p_order_data['extension_end_time'] = time() + (86400 * $promotion_package_info['number']);
                    }else{
                        $p_order_data['is_top'] = 1;
                        $p_order_data['top_end_time'] = time() + (86400 * $promotion_package_info['number']);
                    }

                    switch ($info['promotion_type']){
                        case 1:
                            db('demand')->where('id',$info['promotion_id'])->update($p_order_data);
                            break;
                        case 2:
                            db('want_to_buy')->where('id',$info['promotion_id'])->update($p_order_data);
                            break;
                        case 3:
                            db('service')->where('id',$info['promotion_id'])->update($p_order_data);
                            break;
                    }
                    Db::commit();
                }
            }else{
                Db::rollback();
            }
        }
    }

    /***
     * 会员套餐列表
     */
    public function memberPackageList(){
        $list = db('member_package')->where('status','normal')->select();

        $this->success('',$list);
    }

    //开通会员下单
    public function reMember(){
        if(request()->isPost()){
            $user = $this->auth->getUser();

            $data['member_package_id'] = input('param.member_package_id');
            $member_package_info = db('member_package')->where('id',$data['member_package_id'])->find();
            if(!$member_package_info){
                $this->error('未找到匹配的套餐');
            }
            $money = $member_package_info['money'];
            $data['order_code'] = orderNo();
            $data['createtime'] = time();
            $data['money'] = $money;
            $data['user_id'] = $user->id;
            $data['pay_type'] = input('param.pay_type',1);
            if($data['pay_type'] == 1){
                $notify = request()->domain()."/api/mall/reMemberResult";
                $result = db('member_order')->insert($data);
                if($result){
                    $wechat_result = $this->wechatPayQrcode($data['order_code'],$data['money'],$notify,'微信扫码购买服务');
                    $this->success('',['order_code' => $data['order_code'],'code_url' => $wechat_result['code_url']]);
                }else{
                    $this->error('系统错误,请联系管理员');
                }
            }else{
                $result = db('member_order')->insert($data);
                if($result){
                    $notify_url = request()->domain()."/api/mall/alipayMemberResult";
                    $result = $this->aliPayQrcode($data['order_code'],$data['money'],$notify_url,'支付宝扫码购买服务');
                    $this->success('',$result);
                }else{
                    $this->error();
                }
            }
        }
    }

    //开通会员回调地址
    public function reMemberResult(){
        $postXml = file_get_contents("php://input");    // 接受通知参数；
        Log::info('回调参数查看1');
        Log::info($postXml);
        db('wechat_pay_log')->insert(['text'=>$postXml]);

        $data = json_decode($postXml,true);
        if(isset($data['event_type']) && $data['event_type'] == "TRANSACTION.SUCCESS"){
            $result = controller('WechatPay')->decryptCiphertext($data);
            Log::info('回调参数查看2');
            Log::info($result);

            Db::startTrans();

            $info = db('member_order')->where('order_code',$result['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $member_order['transaction_id'] = $result['transaction_id'];
                $member_order['openid'] = $result['payer']['openid'];
                $member_order['total'] = $result['amount']['total'];
                $member_order['payer_time'] = time();
                $member_order['pay_status'] = 1;

                $p_order = db('member_order')->where('id',$info['id'])->update($member_order);
                if($p_order){
                    $member_package_info = db('member_package')->where('id',$info['member_package_id'])->find();
                    $user_info = db('user')->where('id',$info['user_id'])->find();
                    if($user_info['is_member'] == 1){
                        $member_data['member_end_time'] = $user_info['member_end_time'] + ($member_package_info['number'] * 86400);
                    }else{
                        $member_data['is_member'] = 1;
                        $member_data['member_end_time'] = time() + ($member_package_info['number'] * 86400);
                    }
                    db('user')->where('id',$info['user_id'])->update($member_data);
                    Db::commit();
                }
            }else{
                Db::rollback();
            }
        }
    }

    //支付宝开通会员回调地址
    public function alipayMemberResult(){
        $params = input('param.');
        Log::info('支付宝充值回调参数');
        Log::info($params);
        if($params['trade_status'] == 'TRADE_SUCCESS'){
            Db::startTrans();

            $info = db('member_order')->where('order_code',$params['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $member_order['transaction_id'] = $params['trade_no'];
                $member_order['openid'] = $params['buyer_id'];
                $member_order['total'] = $params['total_amount'];
                $member_order['payer_time'] = time();
                $member_order['pay_status'] = 1;

                $p_order = db('member_order')->where('id',$info['id'])->update($member_order);
                if($p_order){
                    $member_package_info = db('member_package')->where('id',$info['member_package_id'])->find();
                    $user_info = db('user')->where('id',$info['user_id'])->find();
                    if($user_info['is_member'] == 1){
                        $member_data['member_end_time'] = $user_info['member_end_time'] + ($member_package_info['number'] * 86400);
                    }else{
                        $member_data['is_member'] = 1;
                        $member_data['member_end_time'] = time() + ($member_package_info['number'] * 86400);
                    }
                    db('user')->where('id',$info['user_id'])->update($member_data);
                    Db::commit();
                }
            }else{
                Db::rollback();
            }
        }
    }

    /**
     * 需求下单
     */
    public function demandOrder(){
        if(request()->isPost()) {
            $user = $this->auth->getUser();
            Db::startTrans();
            $demand_info = db('demand')->field('id as demand_id,status as d_status,name,title,describe,one_category_id,two_category_id,tree_category_id,province_id,province_code,province_name,city_id,city_code,city_name,region_id,region_code,region_name,years,money as demand_money,tag,logo,imgs,contacts,contacts_tel,content,remarks,user_id as demand_user_id')->where('id',input('param.demand_id'))->lock(true)->find();
            if(!$demand_info){
                $this->error('未找到匹配的商品');
            }

            if($demand_info['d_status'] != 'normal'){
                if($demand_info['d_status'] == 'buy'){
                    $this->error('该商品已经被购买');
                }else if($demand_info['d_status'] == 'frozen'){
                    $info = db('demand_order')->where('status','normal')->where('demand_id',$demand_info['demand_id'])->where('pay_status',2)->where('user_id',$user->id)->find();
                    if(!$info){
                        $this->error('该商品正在被购买');
                    }
                }else{
                    $this->error('商品状态不可被购买');
                }
            }
            unset($demand_info['d_status']);
            $data = $demand_info;
            $money = $demand_info['demand_money'];
            $data['order_code'] = orderNo();
            $data['createtime'] = time();
            $data['money'] = $money;
            $data['user_id'] = $user->id;

            $data['pay_type'] = input('param.pay_type',1);
            if($data['pay_type'] == 1){
                $notify = request()->domain()."/api/mall/demandOrderResult";
                $result = db('demand_order')->insert($data);
                if($result){
                    db('demand')->where('id',$demand_info['demand_id'])->update(['status' => 'frozen']);
                    $wechat_result = $this->wechatPayQrcode($data['order_code'],$data['money'],$notify,'微信扫码购买服务');
                    Db::commit();
                    $this->success('',['order_code' => $data['order_code'],'code_url' => $wechat_result['code_url']]);
                }else{
                    Db::rollback();
                    $this->error('系统错误,请联系管理员');
                }
            }else{
                $result = db('demand_order')->insert($data);
                if($result){
                    db('demand')->where('id',$demand_info['demand_id'])->update(['status' => 'frozen']);
                    $notify_url = request()->domain()."/api/mall/alipayDemandOrderResult";
                    $result = $this->aliPayQrcode($data['order_code'],$data['money'],$notify_url,'支付宝扫码购买服务');
                    Db::commit();
                    $this->success('',$result);
                }else{
                    Db::rollback();
                    $this->error();
                }
            }
        }
    }

    //需求回调地址
    public function demandOrderResult(){
        $postXml = file_get_contents("php://input");    // 接受通知参数；
        Log::info('回调参数查看1');
        Log::info($postXml);
        db('wechat_pay_log')->insert(['text'=>$postXml]);

        $data = json_decode($postXml,true);
        if(isset($data['event_type']) && $data['event_type'] == "TRANSACTION.SUCCESS"){
            $result = controller('WechatPay')->decryptCiphertext($data);
            Log::info('回调参数查看2');
            Log::info($result);

            Db::startTrans();

            $info = db('demand_order')->where('order_code',$result['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $demand_order['transaction_id'] = $result['transaction_id'];
                $demand_order['openid'] = $result['payer']['openid'];
                $demand_order['total'] = $result['amount']['total'];
                $demand_order['payer_time'] = time();
                $demand_order['pay_status'] = 1;+
                db('demand_order')->where('id',$info['id'])->update($demand_order);
                db('demand')->where('id',$info['demand_id'])->update(['status' => 'buy']);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

    //支付宝需求下单回调地址
    public function alipayDemandOrderResult(){
        $params = input('param.');
        Log::info('支付宝充值回调参数');
        Log::info($params);
        if($params['trade_status'] == 'TRADE_SUCCESS'){
            Db::startTrans();

            $info = db('demand_order')->where('order_code',$params['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $demand_order['transaction_id'] = $params['trade_no'];
                $demand_order['openid'] = $params['buyer_id'];
                $demand_order['total'] = $params['total_amount'];
                $demand_order['payer_time'] = time();
                $demand_order['pay_status'] = 1;

                db('demand_order')->where('id',$info['id'])->update($demand_order);
                db('demand')->where('id',$info['demand_id'])->update(['status' => 'buy']);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }


    /**
     * 需求发布内容下单
     */
    public function publishOrder(){
        if(request()->isPost()) {
            $user = $this->auth->getUser();
            $data['pay_type'] = input('param.pay_type');
            $publish_type = input('param.publish_type');
            $order_code = input('param.order_code');
            if(!$order_code){
                $this->error('订单号不存在!');
            }
            Db::startTrans();

            if ($publish_type == 'demand'){
                $order_info = db('demand')->where('order_code',$order_code)->where('user_id',$user->id)->lock(true)->find();
                if(!$order_info){
                    Db::rollback();
                    $this->error('订单不存在!');
                }

                if($data['pay_type'] == 1){
                    $notify = request()->domain()."/api/mall/publishDemandOrderResult";
                    $result = db('demand')->where('id',$order_info['id'])->update($data);
                    if($result){
                        $wechat_result = $this->wechatPayQrcode($order_info['order_code'],$order_info['pay_price'],$notify,'微信扫码');
                        Db::commit();
                        $this->success('',['order_code' => $order_info['order_code'],'code_url' => $wechat_result['code_url']]);
                    }else{
                        Db::rollback();
                        $this->error('系统错误,请联系管理员');
                    }
                }else{
                    $result = db('demand')->where('id',$order_info['id'])->update($data);
                    if($result){
                        $notify_url = request()->domain()."/api/mall/alipayPublishDemandResult";
                        $result = $this->aliPayQrcode($order_info['order_code'],$order_info['pay_price'],$notify_url,'支付宝扫码');
                        Db::commit();
                        $this->success('',$result);
                    }else{
                        Db::rollback();
                        $this->error();
                    }
                }
            }else if($publish_type == 'want_to_buy'){
                $order_info = db('want_to_buy')->where('order_code',$order_code)->where('user_id',$user->id)->lock(true)->find();
                if(!$order_info){
                    Db::rollback();
                    $this->error('订单不存在!');
                }

                if($data['pay_type'] == 1){
                    $notify = request()->domain()."/api/mall/publishWantToBuyOrderResult";
                    $result = db('want_to_buy')->where('id',$order_info['id'])->update($data);
                    if($result){
                        $wechat_result = $this->wechatPayQrcode($order_info['order_code'],$order_info['pay_price'],$notify,'微信扫码');
                        Db::commit();
                        $this->success('',['order_code' => $order_info['order_code'],'code_url' => $wechat_result['code_url']]);
                    }else{
                        Db::rollback();
                        $this->error('系统错误,请联系管理员');
                    }
                }else{
                    $result = db('want_to_buy')->where('id',$order_info['id'])->update($data);
                    if($result){
                        $notify_url = request()->domain()."/api/mall/alipayPublishWantToBuyResult";
                        $result = $this->aliPayQrcode($order_info['order_code'],$order_info['pay_price'],$notify_url,'支付宝扫码');
                        Db::commit();
                        $this->success('',$result);
                    }else{
                        Db::rollback();
                        $this->error();
                    }
                }
            }else if($publish_type == 'service'){
                $order_info = db('service')->where('order_code',$order_code)->where('user_id',$user->id)->lock(true)->find();
                if(!$order_info){
                    Db::rollback();
                    $this->error('订单不存在!');
                }

                if($data['pay_type'] == 1){
                    $notify = request()->domain()."/api/mall/publishServiceOrderResult";
                    $result = db('service')->where('id',$order_info['id'])->update($data);
                    if($result){
                        $wechat_result = $this->wechatPayQrcode($order_info['order_code'],$order_info['pay_price'],$notify,'微信扫码');
                        Db::commit();
                        $this->success('',['order_code' => $order_info['order_code'],'code_url' => $wechat_result['code_url']]);
                    }else{
                        Db::rollback();
                        $this->error('系统错误,请联系管理员');
                    }
                }else{
                    $result = db('service')->where('id',$order_info['id'])->update($data);
                    if($result){
                        $notify_url = request()->domain()."/api/mall/alipayPublishServiceResult";
                        $result = $this->aliPayQrcode($order_info['order_code'],$order_info['pay_price'],$notify_url,'支付宝扫码');
                        Db::commit();
                        $this->success('',$result);
                    }else{
                        Db::rollback();
                        $this->error();
                    }
                }
            }else{
                $this->error('订单类型不正确');
            }
        }
    }

    //----------------------------------------------------------------------------------------想卖--------------------------------------------------------------------------------------//
    //微信发布需求支付回调地址
    public function publishDemandOrderResult(){
        $postXml = file_get_contents("php://input");    // 接受通知参数；
        Log::info('回调参数查看1');
        Log::info($postXml);
        db('wechat_pay_log')->insert(['text'=>$postXml]);

        $data = json_decode($postXml,true);
        if(isset($data['event_type']) && $data['event_type'] == "TRANSACTION.SUCCESS"){
            $result = controller('WechatPay')->decryptCiphertext($data);
            Log::info('回调参数查看2');
            Log::info($result);

            Db::startTrans();

            $info = db('demand')->where('order_code',$result['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $demand['transaction_id'] = $result['transaction_id'];
                $demand['openid'] = $result['payer']['openid'];
                $demand['total'] = $result['amount']['total'];
                $demand['payer_time'] = time();
                $demand['pay_status'] = 1;
                $demand['status'] = 'normal';
                db('demand')->where('id',$info['id'])->update($demand);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

    //支付宝发布需求支付回调地址
    public function alipayPublishDemandResult(){
        $params = input('param.');
        Log::info('支付宝充值回调参数');
        Log::info($params);
        if($params['trade_status'] == 'TRADE_SUCCESS'){
            Db::startTrans();

            $info = db('demand')->where('order_code',$params['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $demand['transaction_id'] = $params['trade_no'];
                $demand['openid'] = $params['buyer_id'];
                $demand['total'] = $params['total_amount'];
                $demand['payer_time'] = time();
                $demand['pay_status'] = 1;
                $demand['status'] = 'normal';

                db('demand')->where('id',$info['id'])->update($demand);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

    //-------------------------------------------------------------------------------------------想买-------------------------------------------------------------------------------------//
    //微信发布想买支付回调地址
    public function publishWantToBuyOrderResult(){
        $postXml = file_get_contents("php://input");    // 接受通知参数；
        Log::info('回调参数查看1');
        Log::info($postXml);
        db('wechat_pay_log')->insert(['text'=>$postXml]);

        $data = json_decode($postXml,true);
        if(isset($data['event_type']) && $data['event_type'] == "TRANSACTION.SUCCESS"){
            $result = controller('WechatPay')->decryptCiphertext($data);
            Log::info('回调参数查看2');
            Log::info($result);

            Db::startTrans();

            $info = db('want_to_buy')->where('order_code',$result['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $want_to_buy['transaction_id'] = $result['transaction_id'];
                $want_to_buy['openid'] = $result['payer']['openid'];
                $want_to_buy['total'] = $result['amount']['total'];
                $want_to_buy['payer_time'] = time();
                $want_to_buy['pay_status'] = 1;
                $want_to_buy['status'] = 'normal';

                db('want_to_buy')->where('id',$info['id'])->update($want_to_buy);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

    //支付宝发布想买支付回调地址
    public function alipayPublishWantToBuyResult(){
        $params = input('param.');
        Log::info('支付宝充值回调参数');
        Log::info($params);
        if($params['trade_status'] == 'TRADE_SUCCESS'){
            Db::startTrans();

            $info = db('want_to_buy')->where('order_code',$params['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $want_to_buy['transaction_id'] = $params['trade_no'];
                $want_to_buy['openid'] = $params['buyer_id'];
                $want_to_buy['total'] = $params['total_amount'];
                $want_to_buy['payer_time'] = time();
                $want_to_buy['pay_status'] = 1;
                $want_to_buy['status'] = 'normal';

                db('want_to_buy')->where('id',$info['id'])->update($want_to_buy);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

    //----------------------------------------------------------------------------------------------------服务-----------------------------------------------------------//

    //微信发布服务支付回调地址
    public function publishServiceOrderResult(){
        $postXml = file_get_contents("php://input");    // 接受通知参数；
        Log::info('回调参数查看1');
        Log::info($postXml);
        db('wechat_pay_log')->insert(['text'=>$postXml]);

        $data = json_decode($postXml,true);
        if(isset($data['event_type']) && $data['event_type'] == "TRANSACTION.SUCCESS"){
            $result = controller('WechatPay')->decryptCiphertext($data);
            Log::info('回调参数查看2');
            Log::info($result);

            Db::startTrans();

            $info = db('service')->where('order_code',$result['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $service['transaction_id'] = $result['transaction_id'];
                $service['openid'] = $result['payer']['openid'];
                $service['total'] = $result['amount']['total'];
                $service['payer_time'] = time();
                $service['pay_status'] = 1;
                $service['status'] = 'normal';

                db('service')->where('id',$info['id'])->update($service);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

    //支付宝发布服务支付回调地址
    public function alipayPublishServiceResult(){
        $params = input('param.');
        Log::info('支付宝充值回调参数');
        Log::info($params);
        if($params['trade_status'] == 'TRADE_SUCCESS'){
            Db::startTrans();

            $info = db('service')->where('order_code',$params['out_trade_no'])->where('pay_status',2)->lock(true)->find();
            if($info){
                $service['transaction_id'] = $params['trade_no'];
                $service['openid'] = $params['buyer_id'];
                $service['total'] = $params['total_amount'];
                $service['payer_time'] = time();
                $service['pay_status'] = 1;
                $service['status'] = 'normal';

                db('service')->where('id',$info['id'])->update($service);
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

    /***
     * 监听需求发布支付
     */
    public function jianTingPublish(){
        $publish_type = input('param.publish_type');
        $order_code = input('param.order_code');
        if(!$order_code){
            $this->error('订单号不存在!');
        }

        if ($publish_type == 'demand'){

            $order_info = db('demand')->where('order_code',$order_code)->find();
        }else if($publish_type == 'want_to_buy'){

            $order_info = db('want_to_buy')->where('order_code',$order_code)->find();
        }else if($publish_type == 'service'){

            $order_info = db('service')->where('order_code',$order_code)->find();
        }else{
            $this->error('订单类型不正确');
        }

        $this->success('',['pay_status' => $order_info['pay_status']]);
    }

    /**
     * 我的购买订单列表
     */
    public function demandOrderList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $list = db('demand_order')->where('user_id',$user->id)->order('id desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
        }
        $this->success('',$data);
    }

    /**
     * 订单详情
     */
    public function demandOrderDetails(){
        $order_code = input('param.order_code');

        $info = db('demand_order')->where('order_code',$order_code)->find();

        $this->success('',$info);
    }

    /**
     * 我的出售订单列表
     */
    public function demandSellOrderList(){
        $user = $this->auth->getUser();

        $page = input('param.page',1);
        $limit = input('param.limit',10);

        $list = db('demand_order')->where('demand_user_id',$user->id)->order('id desc')->paginate($limit,false,['page'=>$page]);
        $data = tp_builde_page($list);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['createtime_text'] = date('Y-m-d H:i:s',$value['createtime']);
        }
        $this->success('',$data);
    }

    /**
     * 申请退款
     */
    public function demandOrderRefund(){
        $user = $this->auth->getUser();
        $order_id = input('param.order_id');

        $info = db('demand_order')->where('user_id',$user->id)->where('id',$order_id)->find();
        if($info['pay_status'] != 1 && $info['status'] != 'normal'){
            $this->error('订单状态不可申请退款');
        }

        $data['refund_time'] = time();
        $data['status'] = 'apply_refund';

        $result = db('demand_order')->where('id',$info['id'])->update($data);

        if($result){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    /**
     * 支付状态确认
     */
    public function payStatus(){
        if(request()->isPost()){
            $user = $this->auth->getUser();
            $order_code = input('param.order_code');
            $table_type = input('param.table_type');

            if(!$table_type || !$order_code){
                $this->error('订单号和查询类型必传');
            }

            $info = db($table_type)->where('user_id',$user->id)->where('order_code',$order_code)->find();
            if($info['pay_status'] == 1){
                $this->success('',['code' => 1]);
            }else{
                $this->success('',['code' => 0]);
            }
        }
    }


}
